package models

import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
	"github.com/spf13/viper"
)

var Ctx = context.Background()
var Rdb *redis.Client

func Inits() {
	viper.SetConfigFile("conf/dev.yaml")
	err = viper.ReadInConfig()
	if err != nil {
		fmt.Println("redis文件读取失败")
		return
	}
	Addr := fmt.Sprintf("%s:%d", viper.GetString("redis.Host"), viper.GetInt("redis.Port"))
	Password := viper.GetString("redis.Password")
	Database := viper.GetInt("redis.Database")
	Rdb = redis.NewClient(&redis.Options{
		Addr:     Addr,
		Password: Password, // no password set
		DB:       Database, // use default DB
	})
	Rdb.Ping(Ctx)
	if err != nil {
		fmt.Println("redis连接失败")
		return
	} else {
		fmt.Println("redis连接成功")
	}

}
